package org.noote.libs.roby.comm;

public class IPCommandProtocol {

	//////////////////////////////////
	///
	/// Client class for IP communication
	///
	/////////////////////////////////
	static final String CMD_START_TRAME = "#";
	static final String CMD_END_TRAME = "$";
	protected static String _bounding(String sCmd, String sOp, String sParams)
	{
		return CMD_START_TRAME+sCmd+((sOp.length()>0)?"_"+sOp:"")+":"+sParams+CMD_END_TRAME+"\n";
	}

	/////////////////////////////////
	// Version
	static final String CMD_QUIT = "quit";
	public static String sendQuit()
	{
		return _bounding(CMD_QUIT, "", "");
	}

	/////////////////////////////////
	// Version
	static final String CMD_VERSION = "version";
	public static String sendHello(String sVersion, String sControllerName)
	{
		return _bounding(CMD_VERSION, "hello", "version="+sVersion+",name="+sControllerName);
	}
	public static String sendAskVersion()
	{
		return _bounding(CMD_VERSION, "", "");
	}

	/////////////////////////////////
	// Setting
	static final String CMD_SETTING = "setting";
	public static String sendAskSetting()
	{
		return _bounding(CMD_SETTING, "ask", "all");
	}
	public static String sendAskSettingBody()
	{
		return _bounding(CMD_SETTING, "ask", "body");
	}
	public static String sendAskSettingTurret()
	{
		return _bounding(CMD_SETTING, "ask", "turret");
	}
	public static String sendAskSettingRay()
	{
		return _bounding(CMD_SETTING, "ask", "ray");
	}
	public static String sendSetSettingRayCount(int iCount)
	{
		return _bounding(CMD_SETTING, "set", "ray_count="+Integer.toString(iCount));
	}
	public static String sendSetSettingRayDelay(int iDelay)
	{
		return _bounding(CMD_SETTING, "set", "ray_delay="+Integer.toString(iDelay));
	}
	public static String sendSetSettingRaySensors(int sSensors)
	{
		return _bounding(CMD_SETTING, "set", "ray_sensors="+Integer.toString(sSensors));
	}
	public static String sendSetSettingRayPrecision(float fPrecision)
	{
		int iP = (int)((fPrecision>1.0f?1:fPrecision<0.0f?0:fPrecision)*100.0f);
		return _bounding(CMD_SETTING, "set", "ray_precision="+Integer.toString(iP));
	}
	/* TODO
	public static String sendSetSettingRay(int iCount, int iDelay, int sSensors, float fPrecision)
	{
		return _bounding(CMD_SETTING+"sra"+Integer.toString(iCount));
	}
	 */

	/////////////////////////////////
	// Status
	static final String CMD_STATUS = "status";
	public static String sendAskStatus()
	{
		return _bounding(CMD_STATUS, "ask", "all");
	}
	public static String sendAskStatusSensors()
	{
		return _bounding(CMD_STATUS, "ask", "sensors");
	}
	public static String sendAskStatusTurret()
	{
		return _bounding(CMD_STATUS, "ask", "turret");
	}
	public static String sendAskStatusBody()
	{
		return _bounding(CMD_STATUS, "ask", "body");
	}
	public static String sendSetSendStatus(boolean bSet)
	{
		return _bounding(CMD_STATUS, "set", "send="+(bSet?"1":"0"));
	}

	/////////////////////////////////
	// Reset
	static final String CMD_RESET = "reset";
	public static String sendReset()
	{
		return _bounding(CMD_RESET, "", "");
	}
	public static String sendResetBody()
	{
		return _bounding(CMD_RESET, "", "body");
	}
	public static String sendResetTurret()
	{
		return _bounding(CMD_RESET, "", "turret");
	}

	/////////////////////////////////
	// Body
	static final String CMD_BODY = "body";
	public static String sendBodyFront(int iSpeed)
	{
		String sSpeed = (iSpeed<0)?"0":(iSpeed>100)?"100":Integer.toString(iSpeed);
		return _bounding(CMD_BODY , "move", "front="+sSpeed);
	}
	public static String sendBodyBack(int iSpeed)
	{
		String sSpeed = (iSpeed<0)?"0":(iSpeed>100)?"100":Integer.toString(iSpeed);
		return _bounding(CMD_BODY, "move", "back="+sSpeed);
	}
	public static String sendBodyLeft(int iSpeed)
	{
		String sSpeed = (iSpeed<0)?"0":(iSpeed>100)?"100":Integer.toString(iSpeed);
		return _bounding(CMD_BODY, "move", "left="+sSpeed);
	}
	public static String sendBodyRight(int iSpeed)
	{
		String sSpeed = (iSpeed<0)?"0":(iSpeed>100)?"100":Integer.toString(iSpeed);
		return _bounding(CMD_BODY, "move", "right"+sSpeed);
	}
	public static String sendBodyMoveToTarget(int iSpeed, boolean bFront, int iLen)
	{
		String sSpeed = (iSpeed<0)?"0":(iSpeed>100)?"100":Integer.toString(iSpeed);
		String sFront = (bFront)?"1":"0";
		String sLen = (iLen<0)?"0":(iLen>100)?"100":Integer.toString(iLen);
		return _bounding(CMD_BODY, "target_move", "front="+sFront+",speed="+sSpeed+",len="+sLen);
	}
	public static String sendBodyRotateToTarget(int iSpeed, int iBearing)
	{
		int iBear = iBearing%360;
		if(iBear<0) iBear+=360;
		
		String sSpeed = (iSpeed<0)?"0":(iSpeed>100)?"100":Integer.toString(iSpeed);
		String sBear = Integer.toString(iBear);
		return _bounding(CMD_BODY, "target_rotate", "speed="+sSpeed+",bearing="+sBear);
	}

	/////////////////////////////////
	// Turret
	static final String CMD_TURRET = "turret";
	public static String sendTurretSetPosition(short low, short high)
	{
		String sLow = Integer.toString(low);
		String sHigh = Integer.toString(high);
		return _bounding(CMD_TURRET, "set", "low="+sLow+",high="+sHigh);
	}
	public static String sendTurretScanPosition(short low, short high)
	{
		String sLow = Integer.toString(low);
		String sHigh = Integer.toString(high);
		return _bounding(CMD_TURRET, "scan_point", "low="+sLow+",high="+sHigh);
	}
	public static String sendTurretScanLine(short low_beg, short low_end, short low_step, short high)
	{
		String sLowBeg = Integer.toString(low_beg);
		String sLowEnd = Integer.toString(low_end);
		String sLowStep = Integer.toString(low_step);
		String sHigh = Integer.toString(high);
		return _bounding(CMD_TURRET, "scan_line", "low_beg="+sLowBeg+",low_end="+sLowEnd+",low_step="+sLowStep+",high="+sHigh);
	}
	public static String sendTurretScanZone(short low_beg, short low_end, short low_step, short high_beg, short high_end, short high_step)
	{
		String sLowBeg = Integer.toString(low_beg);
		String sLowEnd = Integer.toString(low_end);
		String sLowStep = Integer.toString(low_step);
		String sHighBeg = Integer.toString(high_beg);
		String sHighEnd = Integer.toString(high_end);
		String sHighStep = Integer.toString(high_step);
		return _bounding(CMD_TURRET, "scan_zone", "low_beg="+sLowBeg+",low_end="+sLowEnd+",low_step="+sLowStep+",high_beg="+sHighBeg+",high_end="+sHighEnd+",high_step="+sHighStep);
	}

	/////////////////////////////////
	// GPS/Position/Accelerometer...
	static final String CMD_POSITION = "position";
	public static String sendAskPosition()
	{
		return _bounding(CMD_POSITION, "ask", "");
	}

	/////////////////////////////////
	// Camera
	static final String CMD_CAMERA = "camera";
	public static String sendAskCameraImage()
	{
		return _bounding(CMD_CAMERA, "ask", "image");
	}

	/////////////////////////////////
	// Message
	static final String CMD_NOTIFICATION = "notification";
	public static String sendMessage(String sMsg)
	{
		return _bounding(CMD_NOTIFICATION, "message", sMsg);
	}
	public static String sendVibration(String sVib)
	{
		return _bounding(CMD_NOTIFICATION, "vibrate", sVib);
	}
}
